using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Web;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace Paas.Pioneer
{
	public class Program
	{
		public static async Task Main(string[] args)
		{
			var logger = LogManager.GetCurrentClassLogger();
			try
			{
				Console.WriteLine("launching...");
				var host = CreateHostBuilder(args).Build();
				await host.RunAsync();
			}
			catch (Exception ex)
			{
				logger.Error(ex, "Stopped program because of exception");
			}
			finally
			{
				LogManager.Shutdown();
			}
		}

		internal static IHostBuilder CreateHostBuilder(string[] args) =>
			Host.CreateDefaultBuilder(args)
				.ConfigureAppConfiguration(build =>
				{
					build.AddJsonFile("appsettings.secrets.json", optional: true);
					build.AddJsonFile("Configs/appconfig.json", optional: true);
					build.AddJsonFile("Configs/jwtconfig.json", optional: true);
					build.AddJsonFile("Configs/uploadconfig.json", optional: true);
				})
				.ConfigureWebHostDefaults(webBuilder =>
				{
					webBuilder.UseStartup<Startup>();
				})
				.ConfigureLogging(logging =>
				{
					logging.ClearProviders();
					logging.AddNLog("NLog.config");
					logging.SetMinimumLevel(LogLevel.Trace);
				})
				.UseNLog()
				.UseAutofac();
	}
}
